package com.tc.quartz.support.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.tc.quartz.frame.base.BaseController;
import com.tc.quartz.frame.bean.RespBean;
import com.tc.quartz.frame.common.ObjectUtil;
import com.tc.quartz.frame.common.ParamUtil;
import com.tc.quartz.support.bean.JobManagerBean;
import com.tc.quartz.support.service.IJobManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 任务管理
 * @author yongjin.ye
 *
 */
@RequestMapping(value = "/jobManager")
@Controller
public class JobManagerController extends BaseController {

	@Autowired
	private IJobManagerService jobManagerService;
	
	/**
	 * 进入list页面 
	 */
	@RequestMapping(value="/list", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	public String list() {
		return "forward:/jsp/jobManager/list_jobs.jsp";
	}
	
	/**
	 * 列表查询
	 * @return
	 */
	@RequestMapping(value="/list", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String listQuery(JobManagerBean jobBean) {
		int page = ParamUtil.getInt(this.getRequest(), "page", 1);
		int pagesize = ParamUtil.getInt(this.getRequest(), "pagesize", 10);
		PageInfo<JobManagerBean> pageInfo = this.jobManagerService.getPageList(jobBean, page, pagesize);
		for (int i = 0; null!=pageInfo.getList() && i < pageInfo.getList().size(); i++) {
			JobManagerBean jbean  = pageInfo.getList().get(i);
			jbean.setJobDataMap(ObjectUtil.formatJobData2Map(jbean.getDtlJobData()));
		}
		return JSON.toJSONString(pageInfo);
	}
	
	
	@RequestMapping(value="/addJob", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addJob(JobManagerBean jobBean) throws Exception {
		RespBean bean = new RespBean();
		this.jobManagerService.addJob(jobBean);
		bean.setCode("SUCCESS");
		bean.setMessage("任务保存成功！");
		return bean.toJson();
	}
	
	@RequestMapping(value="/deleteJob", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String deleteJob(JobManagerBean jobBean) throws Exception {
		RespBean bean = new RespBean();
		this.jobManagerService.delJob(jobBean);
		bean.setCode("SUCCESS");
		bean.setMessage("任务删除成功！");
		return bean.toJson();
	}
	
	@RequestMapping(value="/pausedJob", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String pausedJob(JobManagerBean jobBean) throws Exception {
		RespBean bean = new RespBean();
		this.jobManagerService.pauseJob(jobBean);
		bean.setCode("SUCCESS");
		bean.setMessage("任务暂停成功！");
		return bean.toJson();
	}
	

	@RequestMapping(value="/resumeJob", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String resumeJob(JobManagerBean jobBean) throws Exception {
		RespBean bean = new RespBean();
		this.jobManagerService.resumeJob(jobBean);
		bean.setCode("SUCCESS");
		bean.setMessage("任务恢复成功！");
		return bean.toJson();
	}
	

	@RequestMapping(value="/triggerJob", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String triggerJob(JobManagerBean jobBean) throws Exception {
		//System.out.println("==============" + jobBean.toJson());
		RespBean bean = new RespBean();
		this.jobManagerService.triggerJob(jobBean);
		bean.setCode("SUCCESS");
		bean.setMessage("任务触发通知成功！");
		return bean.toJson();
	}
	
	@RequestMapping(value="/updateJob", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateJob(JobManagerBean jobBean) throws Exception {
		RespBean bean = new RespBean();
		this.jobManagerService.updateJob(jobBean);
		bean.setCode("SUCCESS");
		bean.setMessage("任务更新成功！");
		return bean.toJson();
	}
	
	@RequestMapping(value="/selectSchedulerState", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	public String selectSchedulerState() throws Exception {
		List<Map<?, ?>> stateList = this.jobManagerService.selectSchedulerState();
		this.getRequest().setAttribute("stateList", stateList);
		return "forward:/jsp/jobManager/list_scheduler_state.jsp";
	}
	
	//INSERT INTO qrtz_fired_triggers (SCHED_NAME, ENTRY_ID, TRIGGER_NAME, TRIGGER_GROUP, INSTANCE_NAME, FIRED_TIME, SCHED_TIME, PRIORITY, STATE, JOB_NAME, JOB_GROUP, IS_NONCONCURRENT, REQUESTS_RECOVERY) VALUES ('scheduler-quartz-manager', 'yongjinye-sh-qa-web-ddd4w14490529090231449052909009', 'MT_1inm026wo6nq3', 'DEFAULT', 'yongjinye-sh-qa-web-ddd4w1449052909023', 1449063157797, 1449063135257, 5, 'EXECUTING', 'test', 'DEFAULT', '1', '0');
	@RequestMapping(value="/selectFiredTriggers", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	public String selectFiredTriggers() throws Exception {
		List<Map<?, ?>> firedList = this.jobManagerService.selectFiredTriggers();
		this.getRequest().setAttribute("firedList", firedList);
		return "forward:/jsp/jobManager/list_fired_triggers.jsp";
	}
	
	
}